package com.querie.util;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.Query;


public class Util {

	public static int getNextId(String field, String className) {
		int result = 1;
		try{
		String sequel = "Select max(" + field + ") from " + className;
		Query q = HibernateUtil.getSessionFactory().openSession()
				.createQuery(sequel);
		List currentSeq = q.list();
		if (currentSeq == null) {
			return result;
		} else {
			result = ((Integer) currentSeq.get(0)) + 1;
			return result;
		}
		}
		catch(Exception ae)
		{
			return result;
		}
	}

	public static int getNextQueryId() {
		int result = getNextId("queryId", "com.querie.model.beans.QueryLog");

		return result;
	}

	public static int getNextSessionId() {
		int result = getNextId("sessionId", "com.querie.model.beans.QueryLog");

		return result;
	}
	
	public static long[] convertStringToLongArray(String str) {
		if(str == null || str.equals("")) {
			return null;
		}
		String[] arString = str.split("::");
		long[] arLong = new long[arString.length]; 
		
		for(int i=0;i<arString.length;i++) {
			arLong[i] = Long.parseLong(arString[i]);
		}
		return arLong;
	}

    public static HashMap<String, Double> sortByComparator(HashMap<String, Double> unsortMap, final boolean order)
    {

    	if (unsortMap.size() <= 0)
    	{
    		return unsortMap;
    	}
        List<Entry<String, Double>> list = new LinkedList<Entry<String, Double>>(unsortMap.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Entry<String, Double>>()
        {
            public int compare(Entry<String, Double> o1,
                    Entry<String, Double> o2)
            {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        // Maintaining insertion order with the help of LinkedList
        HashMap<String, Double> sortedMap = new LinkedHashMap<String, Double>();
        for (Entry<String, Double> entry : list)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }
    
    public static HashMap<Long, Double> sortByComparatorLong(HashMap<Long, Double> unsortMap)
    {

    	if (unsortMap.size() <= 0)
    	{
    		return unsortMap;
    	}
        List<Entry<Long, Double>> list = new LinkedList<Entry<Long, Double>>(unsortMap.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Entry<Long, Double>>()
        {
            public int compare(Entry<Long, Double> o1,
                    Entry<Long, Double> o2)
            {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        // Maintaining insertion order with the help of LinkedList
        HashMap<Long, Double> sortedMap = new LinkedHashMap<Long, Double>();
        for (Entry<Long, Double> entry : list)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println(getNextQueryId());

	}
	
	public static int getNextConfigurationId()
	{
		int result = 1;
		
		File dir  = new File("resources/experiments");
		if (!dir.exists())
		{
			dir.mkdir();
		}
		
		while(true)
		{
			File f  = new File("resources/experiments/configuration"+result);
			
			if (!f.exists())
			{
				f.mkdir();
				break;
			}
			else
			{
				result++;
			}
		}
		return result;
	}
}
